<?php
class SiteAdmin_RegionPageController extends SiteAdminController
{
    
   	public function editAction() {
		$id = (int) $this->getRequest()->getParam('id', 0);
		$addnew = intval($this->getRequest()->getParam('addnew', 0)) === 1 ? true : false;
		
		$arObject = Obj_LocationContentPage::getLocationContentPage($id);
		$current_country = intval($this->getRequest()->getParam('country', false));
    	// get countries
    	$arCountry = Obj_Country::getCountries();
    	$countries = array();
    	$countries[0] = _('Выберите страну...');
    	foreach ($arCountry as $v) {
    		$countries[$v['ID']] = $v['Name'];
    	}    	
    	$this->view->countries = $countries;
    	$this->view->country = ($current_country && $current_country>0)?$current_country:0;
    	
    	// get regions    	
    	$ar = ($current_country && $current_country>0)?Obj_Region::getRegions($current_country):0;
    	$current_region = intval($this->getRequest()->getParam('region', false));
    	$regions = array();
    	$regions[0] = _('Выберите регион...');
        foreach ($ar as $v) {
    		$regions[$v['ID']] = $v['Name'];
    	}    			
    	$this->view->regions = $regions;
    	$this->view->region = ($current_region && $current_region>0)?$arObject['RegionID']:0;    		
		$arCountryPages = Obj_LocationContentPage::getLocationContentPage($id);
		if ($addnew || $arCountryPages === false) {
    		$this->view->cpages = array(
    			'ID' => 0,
    			'ParentID' => '',
    			'ParentType' => '',
    			'Title' => '',
    			'Content' => '',
    			'active' => 0,
    			'seoTitle' => '',
    			'seoDescription' => '',
    			'seoKeywords' => '',
    			'Order' => 0,
    		);
    	}
    	else {
    		$this->view->cpages = $arCountryPages;
    	}
    }

   	public function deleteAction()
    {
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$parent = Obj_LocationContentPage::getLocationContentPage($id);
    	Obj_LocationContentPage::delLocationContentPage($id);
    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_PAGE_CONTENT);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('controller'=>'city', 'action' => 'index', 'region' => $parent['ParentID']))), true, false);  	
    }

    public function saveAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$arCountryPages = array();
    	$id = (int) $this->getRequest()->getParam('ID', 0);
		$arCountryPages['Title'] = trim($this->getRequest()->getParam('Title', ''));
		$getPageByTitle = Obj_LocationContentPage::getLocationContentPage($id);
		$arCountryPages['ParentID'] = (int)intval($this->getRequest()->getParam('ParentID', 0));
		$arCountryPages['Content'] = trim($this->getRequest()->getParam('Content', ''));
		$arCountryPages['active'] = (int)intval($this->getRequest()->getParam('active', 0));
		$arCountryPages['seoTitle'] = trim($this->getRequest()->getParam('seoTitle', ''));
		$arCountryPages['seoDescription'] = trim($this->getRequest()->getParam('seoDescription', ''));
		$arCountryPages['seoKeywords'] = trim($this->getRequest()->getParam('seoKeywords', ''));
		$arCountryPages['ParentType'] = CONTENT_TYPE_REGION;

		$ord = array();
		$_ = Obj_LocationContentPage::getLocationContentPages($arCountryPages['ParentID'], $arCountryPages['ParentType']);
		if(sizeOf($_) > 0){
			foreach ($_ as $v) $ord[] = $v['Order'];
			$num = max($ord)+1;			
		} else {
			$num = 1;
		}
		$arCountryPages['Order'] = intval($this->getRequest()->getParam('Order', $num));
		if(	$arCountryPages['ParentID'] == $getPageByTitle['ParentID'] && 
			$arCountryPages['Title'] == $getPageByTitle['Title'] &&
			$arCountryPages['Content'] == $getPageByTitle['Content'] && 
			$arCountryPages['active'] == $getPageByTitle['active'] && 
			$arCountryPages['seoTitle'] == $getPageByTitle['seoTitle'] && 
			$arCountryPages['seoDescription'] == $getPageByTitle['seoDescription'] && 
			$arCountryPages['seoKeywords'] == $getPageByTitle['seoKeywords'] &&
			(int)$arCountryPages['Order'] == (int)$getPageByTitle['Order']
		){
			$this->_helper->json(array('error' => 1, 'errorStr' => _('Даная страница уже существует. Нет необходимости в повторном добавлении!')), true, false);
		} elseif(!is_int($arCountryPages['ParentID'])){
			$this->_helper->json(array('error' => 1, 'errorStr' => _('Не верно задан идентификатор страны!')), true, false);
		} elseif($arCountryPages['Title'] == ''){
			$this->_helper->json(array('error' => 1, 'errorStr' => _('Задайте заголовок страницы!')), true, false);
		} elseif(strlen($arCountryPages['Title']) > 254){
			$this->_helper->json(array('error' => 1, 'errorStr' => _('Заголовок страницы слишком длинный!')), true, false);
		} elseif(strip_tags($arCountryPages['Content']) == ""){
			$this->_helper->json(array('error' => 1, 'errorStr' => _('Контент страницы не может быть пустым!')), true, false);
		} elseif(!is_int($arCountryPages['Order'])){
			$this->_helper->json(array('error' => 1, 'errorStr' => _('Порядок сортировки задан не верно!')), true, false);
		}
		$arCountryPages['alias'] = Obj_Text::translit($arCountryPages['Title']);
    	$cpid = (int)Obj_LocationContentPage::saveLocationContentPage($arCountryPages, $id);
		Obj_System_SearchIndex::write($cpid, CONTENT_TYPE_PAGE_CONTENT, $arCountryPages['Title'], strip_tags($arCountryPages['Content']));
		$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index', 'module' => 'site-admin', 'controller' => 'city', 'region' => $arCountryPages['ParentID']), null, true)), true, false);
    }
    
    public function stateChangerAction() {
		if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arPage = Obj_LocationContentPage::getLocationContentPage($id);
    	if(intval($arPage['active']) === 1){
    		$arPage['active'] = 0;
    		$rid = Obj_LocationContentPage::saveLocationContentPage($arPage, $id);
    		Obj_System_SearchIndex::delete($rid, CONTENT_TYPE_PAGE_CONTENT);
    	} else {
    		$arPage['active'] = 1;
    		$rid = Obj_LocationContentPage::saveLocationContentPage($arPage, $id);
    		Obj_System_SearchIndex::write($rid, CONTENT_TYPE_PAGE_CONTENT, $arPage['Title'], strip_tags($arPage['Content']));
    	}
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('controller' => 'city', 'action' => 'index', 'region'=>$arPage['ParentID']))), true, false);
    }
    
    
    public function pageStateChangerAction() {
		if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arObjects = Obj_LocationContentPage::getLocationContentPage($id);
    	if(intval($arObjects['active']) === 1){
    		$arObjects['active'] = 0;
    		$oid = Obj_LocationContentPage::saveLocationContentPage($arObjects, $id);
    		Obj_System_SearchIndex::delete($oid, CONTENT_TYPE_PAGE_CONTENT);
    	} else {
    		$arObjects['active'] = 1;
    		$oid = Obj_LocationContentPage::saveLocationContentPage($arObjects, $id);
    		Obj_System_SearchIndex::write($oid, CONTENT_TYPE_PAGE_CONTENT, $arObjects['Title'], $arObjects['Content']);
    	}
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('controller' => 'city', 'action' => 'index', 'region'=>$arPage['ParentID'])), null, false), true, false);
    }

}